package logic

import (
	"context"
	"errors"
	"shoppingcart-srv/model/mysql"
	"shoppingcart-srv/model/redis"

	"shoppingcart-srv/internal/svc"
	"shoppingcart-srv/shoppingcart"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRemoveCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveCartLogic {
	return &RemoveCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RemoveCartLogic) RemoveCart(in *shoppingcart.RemoveCartRequest) (*shoppingcart.RemoveCartResponse, error) {
	// todo: add your logic here and delete this line
	c := mysql.Cart{
		UserId:  in.UserId,
		GoodsId: in.GoodsId,
	}
	err := c.Remove()
	if err != nil {
		return nil, errors.New("购物车移除商品失败")
	}
	err = redis.AddGoodsStock(c.GoodsId, c.Quantity)
	if err != nil {
		return nil, err
	}
	return &shoppingcart.RemoveCartResponse{Success: true}, nil
}
